
require "Common/define"
require "Logic/CtrlManager"
require "Logic/Config"

roleListHandler = roleListHandler or {};
G.serverData = G.serverData or {};

local sub_type = Config.sub_type_roleList


function roleListHandler:receivePacket( p )

	local mainType = WebMsgMgr:getMainType(p);
	local subType = WebMsgMgr:getSubType(p);

	local switch = {  
	    [sub_type.sub_loginGame] = function()  
	       return roleListHandler.decode_loginGame(p);
	    end, 
	    --[sub_type.sub_roleList] = function()
	    --   return roleListHandler.decode_roleList(p);
	    --end,
	    [sub_type.sub_createRole] = function()  
	       return roleListHandler.decode_createRole(p);
	    end, 
	   	[sub_type.sub_roleInfo] = function()  
	       return roleListHandler.decode_enterGame(p);
	    end, 
	    [sub_type.sub_delRole] = function()  
	       return roleListHandler.decode_deleteRole(p);
	    end, 
	    [sub_type.sub_ping] = function()  
	       return roleListHandler.decode_ping(p);
	    end,	   	   
	     
	}  
	  
	local fSwitch = switch[subType]
	  
	if fSwitch then 
	    local data = fSwitch() 
	     if data ~= nil and data.error ~= nil then		    	
			showUI(PanelNames.UISysTips,data.opt,mainType,subType);
			return;
	    end

	    ----记录数据更新，刷新对应ui
	    --local create = CtrlManager.GetCtrl(PanelNames.UICreate);
	    --if create ~= nil then
	    --    create.RefreshUI(data,subType);
	    --end
	else
	  
	end  
end

function roleListHandler.decode_loginGame(p)
	local data = {}
	local opt = WebMsgMgr:getOpt(p)
	data["opt"] = opt
	if opt == 0 or opt == 1 or opt ==2 then
		data["count"] = WebMsgMgr:readByte(p)
		data["profDesc"] = WebMsgMgr:readString(p)
	else
		data["error"] = 1;
	end	

	return data
end

--创建角色解码
function roleListHandler.decode_createRole( p )
	local data = {}
	local opt = WebMsgMgr:getOpt(p)
	data["opt"] = opt
	if opt == 0 then
		data["id"] = WebMsgMgr:getId(p)
		data["roleId"] = WebMsgMgr:readInt(p)
		data["level"] = WebMsgMgr:readInt(p)
	else
		data["error"] = 1;
	end	

	return data
end

--连接游戏服解码
function roleListHandler.decode_connectGame( p )
	local data = {}
	local opt = WebMsgMgr:getOpt(p)
	data["opt"] = opt
	--print("opt"..opt)
	if opt == 0 or opt == 1 or opt == 2	 then
		local count = WebMsgMgr:readByte(p)
		local sub ={}
		for i=1,count do
			local arr = {}
			arr["profDesc"] = WebMsgMgr:readString(p)
			table.insert(sub,arr)
		end
		data["count"] = sub
	end	

	return data
end

--function roleListHandler.decode_roleList(p)
--	local data = {}
--	local size = WebMsgMgr:readByte(p)
--	--print("size"..size)
--	local sub ={}
--	data["size"] = size
--	for i=1,size do
--		local arr = {}
--		arr["id"] = WebMsgMgr:readInt(p)
--
--		arr["Race"] = WebMsgMgr:readByte(p)
--		arr["Profession"] = WebMsgMgr:readByte(p)
--		arr["head"] = WebMsgMgr:readByte(p)
--		arr["gender"] = WebMsgMgr:readByte(p)
--		arr["Level"] = WebMsgMgr:readInt(p)
--		arr["Name"] = WebMsgMgr:readString(p)
--		arr["mapName"] = WebMsgMgr:readString(p)
--
--		arr["itemPicValue"] = roleListHandler.readItemPicValue(p);
--		table.insert(sub,arr)
--	end
--	data["roles"] = sub
--	G.serverData["roleList"] = data;
--	return data
--end

function roleListHandler.readItemPicValue(p)
	local arr = {};
	arr["handState"] = WebMsgMgr:readByte(p)
	for i=1,8 do
		local itemPicValue = {}			
		itemPicValue["isExist"] = WebMsgMgr:readByte(p)
		if itemPicValue["isExist"] == 1 then
			itemPicValue["imgFile"] = WebMsgMgr:readString(p)
			itemPicValue["binFile"] = WebMsgMgr:readString(p)
		end
		arr["itemPicValue"..i] = itemPicValue
	end
	return arr;
end

function roleListHandler.decode_deleteRole(p)
	local data = {};
	local opt = WebMsgMgr:getOpt(p);
	data["opt"] = opt;
	if opt == 1 then
		data["error"] = 1;
	end
	return data;
end


--进入游戏服
function roleListHandler.decode_enterGame( p )

	local data = {}
	local opt = WebMsgMgr:getOpt(p)
	data["opt"] = opt
	data["ability"] = {};
	if opt == 0 then
		local ability = {}
		ability["RoleId"] = WebMsgMgr:readInt(p)
		ability["Name"] = WebMsgMgr:readString(p)
		ability["race"] = WebMsgMgr:readByte(p)
		ability["prof"] = WebMsgMgr:readByte(p)
		ability["head"] = WebMsgMgr:readByte(p)
		ability["gender"] = WebMsgMgr:readByte(p)
		ability["level"] = WebMsgMgr:readInt(p)
		--ability["peakLevel"] = WebMsgMgr:readInt(p)
		ability["exp"] = WebMsgMgr:readInt(p)
		ability["nextExp"] = WebMsgMgr:readInt(p)
		ability["curLife"] = WebMsgMgr:readInt(p)
		ability["baseLife"] = WebMsgMgr:readInt(p)
		ability["exdLife"] = WebMsgMgr:readInt(p)
		ability["lifePer"] = WebMsgMgr:readShort(p)
		ability["curMana"] = WebMsgMgr:readInt(p)
		ability["baseMana"] = WebMsgMgr:readInt(p)
		ability["exdMana"] = WebMsgMgr:readInt(p)
		ability["manaPer"] = WebMsgMgr:readShort(p)
		ability["str"] = WebMsgMgr:readInt(p)
		ability["dex"] = WebMsgMgr:readInt(p)
		ability["eng"] = WebMsgMgr:readInt(p)
		ability["vit"] = WebMsgMgr:readInt(p)
		ability["propPoints"] = WebMsgMgr:readShort(p)
		ability["skillPoints"] = WebMsgMgr:readShort(p)
		ability["mf"] = WebMsgMgr:readShort(p)
		ability["ee"] = WebMsgMgr:readShort(p)
		ability["eg"] = WebMsgMgr:readShort(p)

		data["ability"] = ability;

		local roleData = {};
		roleData["att"]=WebMsgMgr:readInt(p);
		roleData["att_per"]=WebMsgMgr:readShort(p);
		roleData["ac"]=WebMsgMgr:readInt(p);
		roleData["ac_per"]=WebMsgMgr:readShort(p);
		roleData["br"]=WebMsgMgr:readByte(p);
		roleData["dr"]=WebMsgMgr:readShort(p);
		roleData["mdr"]=WebMsgMgr:readShort(p);
		roleData["res_fire"]=WebMsgMgr:readShort(p);
		roleData["res_ice"]=WebMsgMgr:readShort(p);
		roleData["res_lighting"]=WebMsgMgr:readShort(p);
		roleData["res_poison"]=WebMsgMgr:readShort(p);
		roleData["res_dmg"]=WebMsgMgr:readShort(p);
		roleData["res_mag"]=WebMsgMgr:readShort(p);
		roleData["res_maxfire"]=WebMsgMgr:readShort(p);
		roleData["res_maxice"]=WebMsgMgr:readShort(p);
		roleData["res_maxlighting"]=WebMsgMgr:readShort(p);
		roleData["res_maxpoison"]=WebMsgMgr:readShort(p);
		roleData["res_maxdmg"]=WebMsgMgr:readShort(p);
		roleData["res_maxmag"]=WebMsgMgr:readShort(p);
		roleData["regen_hp"]=WebMsgMgr:readShort(p);
		roleData["regen_mp"]=WebMsgMgr:readShort(p);
		roleData["baseIas"]=WebMsgMgr:readShort(p);
		roleData["baseFcr"]=WebMsgMgr:readShort(p);
		roleData["ias"]=WebMsgMgr:readByte(p);
		roleData["fcr"]=WebMsgMgr:readByte(p);
		roleData["ds"]=WebMsgMgr:readByte(p);
		roleData["cb"]=WebMsgMgr:readShort(p);
		roleData["ow"]=WebMsgMgr:readByte(p);
		roleData["ll"]=WebMsgMgr:readByte(p);
		roleData["ml"]=WebMsgMgr:readByte(p);
		roleData["itd"]=WebMsgMgr:readByte(p);
		roleData["absorb_alldmg"]=WebMsgMgr:readByte(p);
		roleData["dmg_to_mana"]=WebMsgMgr:readByte(p);
		roleData["fire_min"]=WebMsgMgr:readInt(p);
		roleData["fire_max"]=WebMsgMgr:readInt(p);
		roleData["fire_per"]=WebMsgMgr:readShort(p);
		roleData["cold_min"]=WebMsgMgr:readInt(p);
		roleData["cold_max"]=WebMsgMgr:readInt(p);
		roleData["cold_per"]=WebMsgMgr:readShort(p);
		roleData["lighting_min"]=WebMsgMgr:readInt(p);
		roleData["lighting_max"]=WebMsgMgr:readInt(p);
		roleData["lighting_per"]=WebMsgMgr:readShort(p);
		roleData["poison_min"]=WebMsgMgr:readInt(p);
		roleData["poison_max"]=WebMsgMgr:readInt(p);
		roleData["poison_per"]=WebMsgMgr:readShort(p);
		roleData["damage_min"]=WebMsgMgr:readInt(p);
		roleData["damage_max"]=WebMsgMgr:readInt(p);
		roleData["magic_min"]=WebMsgMgr:readInt(p);
		roleData["magic_max"]=WebMsgMgr:readInt(p);
		roleData["magic_per"]=WebMsgMgr:readShort(p);
		roleData["master_weapon_min_d"]=WebMsgMgr:readInt(p);
		roleData["master_weapon_max_d"]=WebMsgMgr:readInt(p);
		roleData["master_damage_per"]=WebMsgMgr:readShort(p);
		roleData["weapon_min_d"]=WebMsgMgr:readInt(p);
		roleData["weapon_max_d"]=WebMsgMgr:readInt(p);
		roleData["damage_per"]=WebMsgMgr:readShort(p);
		roleData["dmg_undead"] = WebMsgMgr:readShort(p);
		roleData["dmg_demon"] = WebMsgMgr:readShort(p);
		roleData["pierceFire"]=WebMsgMgr:readShort(p);
		roleData["pierceCold"]=WebMsgMgr:readShort(p);
		roleData["pierceLighting"]=WebMsgMgr:readShort(p);
		roleData["piercePoison"]=WebMsgMgr:readShort(p);
		roleData["pierceDmg"]=WebMsgMgr:readShort(p);
		roleData["pierceMagic"]=WebMsgMgr:readShort(p);
		roleData["pierceAcPer"]=WebMsgMgr:readShort(p);
		roleData["pierceAc"]=WebMsgMgr:readShort(p);
		roleData["pierceAttPer"]=WebMsgMgr:readShort(p);
		roleData["pierceAtt"]=WebMsgMgr:readShort(p);
		roleData["pierceIas"]=WebMsgMgr:readShort(p);
		roleData["pierceFcr"]=WebMsgMgr:readShort(p);
		roleData["allSkills"]=WebMsgMgr:readByte(p);
		roleData["stunbreak"]=WebMsgMgr:readByte(p);
		roleData["freezeBreak"]=WebMsgMgr:readByte(p);
		roleData["soporBreak"]=WebMsgMgr:readByte(p);
		roleData["silenceBreak"]=WebMsgMgr:readByte(p);
		roleData["duranceBreak"]=WebMsgMgr:readByte(p);
		roleData["redPoisLen"]=WebMsgMgr:readByte(p);
		local skillTabCount = WebMsgMgr:readByte(p);
		roleData["skillTabCount"]=skillTabCount
		
		local skillTab = {};
		for i=1,skillTabCount do
			local arr = {};
			arr["TabId"] = WebMsgMgr:readShort(p);
			arr["lvl"] = WebMsgMgr:readShort(p);
			table.insert(skillTab,arr);
		end
		roleData["skillTab"] = skillTab;

		local exdSkillCount = WebMsgMgr:readByte(p);
		roleData["exdSkillCount"]=exdSkillCount
		local exdSkill = {};
		for i=1,exdSkillCount do
			local arr = {};
			arr["SkillId"] = WebMsgMgr:readShort(p);
			arr["lvl"] = WebMsgMgr:readShort(p);

			table.insert(exdSkill,arr);
		end
		roleData["exdSkill"] = exdSkill;

		roleData["coldlen"] = WebMsgMgr:readInt(p);
		roleData["poislen"] = WebMsgMgr:readInt(p);

		data["roleData"] = roleData		
	else
		data["error"] = 1;
	end
	
	--mapMgr:ResurrectionHero(result);--人物复活
	
	if data.opt == 0 then
		--技能点变化刷新技能界面
		local oldPoint = data.ability.skillPoints or 0;
		if G.serverData["roleInfo"] ~= nil then
			oldPoint = G.serverData["roleInfo"].ability.skillPoints;
		end	
		
		--等级变化
		local oldLevel = data.ability.level or 0;
		local props  =  data.ability.propPoints or 0;
		if G.serverData["roleInfo"] ~= nil then
			oldLevel = G.serverData["roleInfo"].ability.level;
			props = data.ability.propPoints -  G.serverData["roleInfo"].ability.propPoints;
		end	
		
		
		G.serverData["roleInfo"] = data;
		
		if oldPoint ~= data.ability.skillPoints  then
			local name = getProtocol(Config.msg_main_type.main_skillInfo,Config.sub_type_skill.sub_addSkillPoint);
			FireEvent(name,nil);
		end
		
		if oldLevel ~= data.ability.level then
			local d = {};
			d.oldLevel = oldLevel;
			d.level = data.ability.level;
			d.skillPoints = data.ability.skillPoints-oldPoint;
			d.upProps = props;
			FireEvent("LevelUp",d);
		end
		
		
		local json = require 'Common/dkjson';
		local result=json.encode(data);
		AppFacade:SendMessageCommand("ON_UPDATE_ROLE_JSON",tostring(result) );
	end
    if G.serverData["roleInfo"] ~= nil and G.serverData["roleInfo"]["ability"] ~= nil then
        --IMAudioMgr:loginIMSystem(G.serverData["roleInfo"]["ability"].RoleId);

        if(G.serverData["roleInfo"] ~= nil and G.serverData["roleInfo"].ability.level >= 40) then
        end

    end
	return data
end

function roleListHandler.decode_ping(msg)
	-- body
	--Game.sendPingTime = Config.serverTime;
    print("收到心跳返回。。。。");

end
